<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from '../../dashboard/mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null,
      colors:["#22c119","#ffba0c","#10a0ff","#de94de","#d83006"],
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      // this.chartData = {
      //   data: [
      //     [100, 120, 161, 134, 105, 160, 165],
      //     [120, 82, 91, 154, 162, 140, 145],
      //   ],
      //   legend: ['类型a','类型b'],
      //   xdata: ['指标1', '指标2', '指标3', '指标4', '指标5', '指标6', '指标7']
      // }
      console.log(this.chartData)
      this.setOptions(this.chartData)
    },
    setOptions(chartData) {
      var series = []
      for(var i=0;i<chartData.legend.length;i++){
        var ser ={
          name: chartData.legend[i],
          itemStyle: {
            normal: {
              color: this.colors[i],
              lineStyle: {
                color: this.colors[i],
                width: 2
              }
            }
          },
          smooth: true,
          type: 'line',
          symbolSize: 4,
          data: chartData.ydata[i],
          // animationDuration: 2800,
          // animationEasing: 'cubicInOut'
        }
        series.push(ser)
      }
      this.chart.setOption({
        xAxis: {
          data: chartData.xdata,
          boundaryGap: false,
          axisTick: {
            show: false
          },
          axisLabel: {
            textStyle: {
              color: '#333',
            },
          },
          axisLine: {
            lineStyle: {
              color: '#D9D9D9',
            },
          }
        },
        grid: {
          left: "5%",
          right: "5%",
          bottom: 20,
          top: 30,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow',
          },
        },
        yAxis: {
          axisTick: {
            show: false
          },
          axisLabel: {
            textStyle: {
              color: '#333',
            },
          },
          axisLine: {
            lineStyle: {
              color: '#D9D9D9',
            },
          }
        },
        legend: {
          show: true,
          data: chartData.legend
        },
        series: series
      },true)
    }
  }
}
</script>
